package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.ActivityCollector;
import com.dlwx.firefighting.utils.CacheManager;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.PhoneInfoUtil;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.flyco.dialog.widget.NormalDialog;
import com.tencent.bugly.beta.Beta;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * 用户设置界面
 */
public class UserSettingActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.rel_change_psw)
    AutoRelativeLayout rel_change;
    @BindView(R.id.tv_cache_size)
    TextView tvCacheSize;
    @BindView(R.id.rel_clearcache)
    AutoRelativeLayout relClearcache;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.rel_version)
    AutoRelativeLayout relVersion;
    @BindView(R.id.rel_loginout)
    AutoLinearLayout relLoginout;
    @BindView(R.id.rel_pay_psw)
    AutoRelativeLayout relPayPsw;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.rl_username)
    AutoRelativeLayout rlUsername;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.rl_phone)
    AutoRelativeLayout rlPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.rl_address)
    AutoRelativeLayout rlAddress;

    private String size;
    private boolean isMember;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_setting);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText(getResources().getString(R.string.setting));

        //设置当前版本号
        String version = PhoneInfoUtil.getInstance().getVersion(UserSettingActivity.this);
        if (TextUtils.isEmpty(version)) {
            tvVersion.setText("");
        } else {
            tvVersion.setText(version);
        }

        //查看缓存大小
        try {
            size = CacheManager.getTotalCacheSize(getApplicationContext());
            if (size != null) {
                tvCacheSize.setText(size);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.title_left_one_btn, R.id.rel_change_psw, R.id.rel_clearcache, R.id.rel_version
            , R.id.rel_loginout, R.id.rel_pay_psw,R.id.rl_username, R.id.rl_phone, R.id.rl_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                finish();
                break;
            case R.id.rel_change_psw:
                startActivity(new Intent(UserSettingActivity.this, ResetPassWordActivity.class));
                break;
            case R.id.rel_clearcache:
                String[] stringItems = {"清除缓存"};
                final ActionSheetDialog sheetDialog = DialogUtils.getBottomSheetDialog(this,
                        "确定要清除缓存数据吗？", stringItems);
                sheetDialog.setOnOperItemClickL(new OnOperItemClickL() {
                    @Override
                    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0) {
                            CacheManager.clearAllCache(getApplicationContext());
                            ToastUtils.toastForShort(UserSettingActivity.this, "清除缓存成功");
                            tvCacheSize.setText("0M");
                        }
                        sheetDialog.dismiss();
                    }
                });
                break;
            case R.id.rel_version:
                Beta.checkUpgrade();
                break;
            case R.id.rel_loginout:
                logOut();
                break;
            case R.id.rel_pay_psw:
                startActivity(new Intent(UserSettingActivity.this, PayPswActivity.class));
                break;
            case R.id.rl_username:
                break;
            case R.id.rl_phone:
                break;
            case R.id.rl_address:
                startActivity(new Intent(UserSettingActivity.this, AddressListActivity.class));
                break;
        }
    }

    private boolean isRemember;
    private String name;
    private String psw;
    NormalDialog dialog;

    /**
     * 退出登录
     */
    private void logOut() {
        dialog = DialogUtils.getNormalDialog(UserSettingActivity.this, "提示", "是否退出登录?");
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
                isRemember = (boolean) SPUtils.get(UserSettingActivity.this, Constant.IS_REMEMBER, false);
                isMember = (boolean) SPUtils.get(UserSettingActivity.this, Constant.IS_MEMBER, false);
                name = (String) SPUtils.get(UserSettingActivity.this, Constant.LOGIN_NAME, "");
                psw = (String) SPUtils.get(UserSettingActivity.this, Constant.LOGIN_PSW, "");
                SPUtils.clear(UserSettingActivity.this);
                SPUtils.put(UserSettingActivity.this, Constant.isLogin, false);
                SPUtils.put(UserSettingActivity.this, Constant.IS_REMEMBER, isRemember);
                SPUtils.put(UserSettingActivity.this, Constant.LOGIN_NAME, name);
                SPUtils.put(UserSettingActivity.this, Constant.LOGIN_PSW, psw);
                SPUtils.put(UserSettingActivity.this, Constant.IS_MEMBER, isMember);
                JPushInterface.deleteAlias(UserSettingActivity.this, Constant.SUCESS);
                Intent intent = new Intent(UserSettingActivity.this, LoginActivity.class);
                startActivity(intent);
                ActivityCollector.getInstances().finishAllActivity();
//                    UserSettingActivity.this.finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog != null) {
            dialog.dismiss();
        }
    }
    //----------------------------------------------------------------
}
